package com.cliave.ttweb.service.impl;

import com.cliave.ttweb.bean.DishesCategory;
import com.cliave.ttweb.exception.GlobalException;
import com.cliave.ttweb.mapper.DishCategoryMapper;
import com.cliave.ttweb.mapper.DishMapper;
import com.cliave.ttweb.service.DishCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class DishCategoryServiceImpl implements DishCategoryService {
    @Autowired
    private DishCategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Override
    public List<DishesCategory> findDishCategoryAll() {
        List<DishesCategory> list = categoryMapper.findDishCategoryAll();
        return list;
    }
    @Transactional
    /**
     * @Transactional注释-----myspl的事务管理
     * 可以让sql语句同时成功或失败，防止运行了一句中间报错导致第二句没运行
     * 一般加在有多条sql语句的方法下面
     */
    @Override
    public int deleteDishCategory(Integer id) {
       int i = dishMapper.deleteDishByCategoryId(id);
       int j = categoryMapper.deleteDishCategory(id);
       if (j<=0){
           throw new GlobalException("删除菜品分类失败");
       }
        return j;
    }
}
